//
//  PSConfirmation.m
//  PrefCore
//
//  Created by Sergey on 2/3/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "PSConfirmation.h"

static int deltasPlay[] = {2, 1, 0};

static int deltasPass[] = {2, 1, 0};

BOOL PSConfirmPlay(PSGameConfirmation confirmation, PSGame* game, PSGameSettings* settings, PSBid* bid) {
    assert(bid.isPlay); // This function is to be called only with play bids
    if (confirmation == kConfirmationAlways) {
        return YES;
    }
    if (confirmation == kConfirmationNever || game.rounds != 0) {
        return NO;
    }
    PSTrickCount estimated = [game estimateHumanTrickCountWithSettings:settings];
    int index = bid.tricks - 6;
    assert(index >= 0); // Play bids cannot be for less than six tricks
    return index > 2 || (estimated < bid.tricks && bid.tricks - estimated > deltasPlay[index]);
}

BOOL PSConfirmPass(PSGameConfirmation confirmation, PSGame* game, PSGameSettings* settings, PSBid* bid) {
    if (confirmation == kConfirmationAlways) {
        return YES;
    }
    if (confirmation == kConfirmationNever || game.rounds != 0) {
        return NO;
    }
    PSTrickCount estimated = [game estimateHumanTrickCountWithSettings:settings];
    PSBid *minBid = game.options.bid;
    int index = minBid.tricks - 6;
    assert(index >= 0); // Minimum bids cannot be for less than six tricks
    return index > 2 || minBid.tricks - estimated <= deltasPass[index];
}

BOOL PSConfirmMisere(PSGameConfirmation confirmation, PSGame* game, PSGameSettings* settings, PSBid* bid) {
    if (confirmation == kConfirmationAlways) {
        return YES;
    }
    if (confirmation == kConfirmationNever || game.rounds != 0) {
        return NO;
    }
    return [game estimateHumanTrickCountWithSettings:settings] != 0;
}

BOOL PSConfirmWithoutThree(PSGameConfirmation confirmation, PSGame* game, PSGameSettings* settings, PSBid* bid) {
    if (confirmation == kConfirmationAlways) {
        return YES;
    }
    if (confirmation == kConfirmationNever || game.rounds != 0) {
        return NO;
    }
    PSBid* estimated = [game estimateHumanContract];
    PSBid* min = game.options.bid;
    return estimated.isPlay && min.tricks > estimated.tricks + 1;
}

BOOL PSConfirmContract(PSGameConfirmation confirmation, PSGame* game, PSGameSettings* settings, PSBid* bid) {
    if (confirmation == kConfirmationAlways) {
        return YES;
    }
    if (confirmation == kConfirmationNever || game.hand != 0) {
        return NO;
    }
    return bid != [game estimateHumanContract];
}
